package com.netty.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 时间服务器
 */
public class TimeServerBio {

    //    public static void main(String[] args)  {
//        // 创建服务 监听端口
//        ServerSocket serverSocket = null;
//        try {
//            serverSocket = new ServerSocket(8080);
//            System.out.println("TimeServerBio start port:"+serverSocket.getLocalPort());
//            while (true) {
//                System.out.println("TimeServerBio accept");
//                Socket socket = serverSocket.accept();
//                // 单线程
//                new Thread(new TimeServerHandlerBio(socket)).start();
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//           if(serverSocket!=null){
//               try {
//                   serverSocket.close();
//               } catch (IOException e) {
//                   e.printStackTrace();
//               }
//           }
//        }
//
//    }
    public static void main(String[] args) {
        // 创建服务 监听端口
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8080);
            System.out.println("TimeServerBio start port:" + serverSocket.getLocalPort());
            while (true) {
                System.out.println("TimeServerBio accept");
                Socket socket = serverSocket.accept();
                // 线程池
                TimeServerHandlerExecutePool timeServerHandlerExecutePool = new TimeServerHandlerExecutePool(50, 1000);
                timeServerHandlerExecutePool.execute(new TimeServerHandlerBio(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
